package com.zjut.zs.util.tools;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;


public class PropertiesUtil {
    
    private static Properties proper = new Properties();
    static{
        try {
        	InputStream in = new FileInputStream(Thread.currentThread()
					.getContextClassLoader().getResource("pay.properties").getPath()
					.toString().replace("%20", " "));
            proper.load(in);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
     
    public static String getStringValue(String  key,String defultValue){
        String value = proper.getProperty(key);
        if(isEmpty(value)){
            return defultValue;
        }
        return value;
    }
     
    public static boolean getBooleanValue(String key,boolean defultValue){
        String value = proper.getProperty(key);
        if(isEmpty(value)){
            return defultValue;
        }
        return Boolean.parseBoolean(value);
    }
     
    public static Integer getIntegerValue(String key,Integer defultValue){
        String value = proper.getProperty(key);
        if(isEmpty(value)){
            return defultValue;
        }
        return Integer.parseInt(value);
    }
    /**
     * 读取所有的配置文件信息
     * */
    public static Map<String, String> getAllProperties(){
        Map<String, String> map = new HashMap<String, String>();
        Enumeration en = proper.propertyNames();
        String key  = "";
        String value = "";
        while(en.hasMoreElements()){
            key = (String)en.nextElement();
            value = proper.getProperty(key);
            map.put(key, value);
        }
        return map;
    }

	private static boolean isEmpty(String value) {
		if (value==null || value.trim().length()==0) {
			return true;
		}
		return false;
	}
}
